<?php
namespace Member\Controller;
use Think\Controller;
class WarehouseController extends BaseController {
    public function index(){
        if(I("get.name")!==null && I("get.name")!==''){
            $where['name']=I("get.name");
        }
		$where['member']=session('member');

        $warehouse = M('warehouse');
		$department = M('DepartmentWarehouse');
        $count      = $warehouse->where($where)->group("warehouse_code")->count();
        $Page       = new \Think\Page($count,15);
        $show       = $Page->show();
        $list = $warehouse->where($where)->limit($Page->firstRow.','.$Page->listRows)->order("id desc")->group("warehouse_code")->select();
		// echo $warehouse->getlastsql();
		foreach($list as $k=>$v){
			$department_name=$department->where(array('id'=>$v['department_id']))->find();
			$list[$k]['department_name']=$department_name['name'];
		}
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('count',$count);
        $this->display();
    }
	//绑定角色
	public function bind_role_warehouse(){
		$m=M('DepartmentWarehouse');
		$list=$m->select();
		$this->assign('list',$list);
		$this->display();
	}
	public function confirm_bind_role_warehouse(){
		$m=M('Warehouse');
		$ids=I('ids');
		$ids=explode(',',$ids);
		$role_id=I('role_id');
		foreach($ids as $k=>$v){
			$data['department_id']=$role_id;
			$res=$m->where(array('id'=>$v))->save($data);
			if($res===false){
				$this->error('更新出错');
			}else{
				//同步角色
				continue;
			}
		}
		$this->success('更新成功');
	}
    public function addD($name,$code){
        if($name==''){
            alert(0,'仓库名称不能为空');
        }
        if($code==''){
            alert(0,'仓库编码不能为空');
        }
        $data['name']=$name;
        $data['warehouse_code']=$code;
        $data['_logic'] = 'or';
        if(M("warehouse")->where($data)->count()>0){
            alert(0,'仓库名称或者仓库编码已经存在');
        }
        unset($data['_logic']);
        $data['operator']=session('admin');
        $result= M("warehouse")->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function editD($id,$name){
        if($name==''){
            alert(0,'单位名称不能为空');
        }
        if($id==''){
            alert(0,'操作有误');
        }
        $data['name']=trim($name);
        $data['operator']=session('admin');
        $result= M("warehouse")->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function del($id){
        if($id==''){
            alert(0,'操作有误');
        }
        $group_id=explode('@',$id);
        $id='';
        foreach($group_id as $v){
            if($v!==''){
                $id.=','.$v;
            }
        }
        $result= M("warehouse")->delete(ltrim($id,','));
        if($result){
            alert(1,'删除成功');
        }else{
            alert(0,'删除失败');
        }
    }
    public function interfaces($id){
        if(I("get.name")!==null && I("get.name")!==''){
            $where['name']=array('like','%'.I("get.name").'%');
        }else{
            $where="1=1";
        }
        if($id==''){
            alert(0,'操作有误');
        }
        $interfaces = M('interfaces');
        $count      = $interfaces->where($where)->where("warehouse_id='".$id."'")->count();
        $Page       = new \Think\Page($count,15);
        $show       = $Page->show();
        $list = $interfaces->where($where)->where("warehouse_id='".$id."'")->limit($Page->firstRow.','.$Page->listRows)->order("vars")->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('id',$id);
        $this->assign('count',$count);
        $this->display();
    }
    public function interfacesAddD($id,$name,$url,$vars){
        if($name==''){
            alert(0,'接口名称不能为空');
        }
        if($url==''){
            alert(0,'接口地址不能为空');
        }
        if($vars==''){
            alert(0,'变量名称不能为空');
        }
        if(!is_my_var($vars)){
            alert(0,'变量名称不能含有英文和数字以及下划线以外的字符，并且以英文开头');
        }
        if(!is_url($url)){
            alert(0,'url地址错误');
        }
        if(M("warehouse")->where("id='".$id."'")->count()==0){
            alert(0,'操作有误');
        }
        $data['url']=$url;
        $data['vars']=$vars;
        $data['_logic'] = 'or';
        $m['_complex']=$data;
        $m['warehouse_id']=array('eq',$id);
        if(M("interfaces")->where($m)->count()>0){
            alert(0,'变量名称或者URL已经存在');
        }
        unset($data['_logic']);
        $data['name']=$name;
        $data['warehouse_id']=$id;
        $data['operator']=session('admin');
        $result= M("interfaces")->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function interfacesEditD($id,$name,$url,$vars,$warehouse_id){
        if($name==''){
            alert(0,'接口名称不能为空');
        }
        if($url==''){
            alert(0,'接口地址不能为空');
        }
        if($vars==''){
            alert(0,'变量名称不能为空');
        }
        if(!is_my_var($vars)){
            alert(0,'变量名称不能含有英文和数字以及下划线以外的字符，并且以英文开头');
        }
        if(!is_url($url)){
            alert(0,'url地址错误');
        }
        $data['url']=$url;
        $data['vars']=$vars;
        $data['_logic'] = 'or';
        $m['_complex']=$data;
        $m['warehouse_id']=array('eq',$warehouse_id);
        $m['id']=array('neq',$id);
        if(M("interfaces")->where($m)->count()>0) {
            alert(0, '变量名称或者URL已经存在');
        }
        unset($data['_logic']);
        $data['name']=trim($name);
        $data['operator']=session('admin');
        $result= M("interfaces")->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function interfacesDel($id){
        if($id==''){
            alert(0,'操作有误');
        }
        $group_id=explode('@',$id);
        $id='';
        foreach($group_id as $v){
            if($v!==''){
                $id.=','.$v;
            }
        }
        $result= M("interfaces")->delete(ltrim($id,','));
        if($result){
            alert(1,'删除成功');
        }else{
            alert(0,'删除失败');
        }
    }
	//升级数据库
	public function updateAll(){
		$m=M('Warehouse');
		$list=$m->where(array('warehouse_code'=>array('neq','41494474')))->order('id asc')->select();
		// echo $m->getlastsql();
		// die;
		require_once('./cloudAPP/Admin/Common/DBManage.class.php');
		foreach($list as $k=>$v){
			$dbname=$v['db_name'];//数据库名称
			echo "<div style='font-size:12px;'><br>当前操作仓库：".$v['name']."<br><br><br>";
			//使用读取写入方式导入
			$db = new \DBManage(C('DB_HOST'),C('DB_USER'),C('DB_PWD'),$dbname,'utf8');
			//导入
			$db->restore('./data/uw.sql');
			unset($db);
		}
		@unlink('./data/update.sql');//删除文件
    }
	//清空数据库
	public function truncateAll(){
		$m=M('Warehouse');
		$id=I('id');
		$warehouse_info=$m->where(array('id'=>$id))->find();
		require_once('./cloudAPP/Admin/Common/DBManage.class.php');
		$dbname=$warehouse_info['db_name'];//数据库名称
		echo "<div style='font-size:12px;'><br>当前操作仓库：".$warehouse_info['name']."<br><br><br>";
		//使用读取写入方式导入
		$db = new \DBManage(C('DB_HOST'),C('DB_USER'),C('DB_PWD'),$dbname,'utf8');
		//导入
		$db->restore('./data/truncate_warehouse.sql');
		unset($db);
    }
	//角色列表
    public function department(){
        $f=C('DB_PREFIX');
        $list = M('DepartmentWarehouse')->order('created_at desc')->select();
        $this->assign('list',$list);
        $this->display();
    }
	public function department_add(){
		$root=M('RootWarehouse')->order("sort")->select();
        $new_arr=array();
		foreach($root as $k=>$v){
            if($v['pid']==0){//顶级菜单
                foreach($root as $j=>$u){
					if($v['id']==$u['pid']){
						foreach($root as $js=>$us){
							if($u['id']==$us['pid']){
								foreach($root as $jss=>$uss){
									if($us['id']==$uss['pid']){
											$us['child'][]=$uss;
									}
								}
								$u['child'][]=$us;
							}
						}
						$v['child'][]=$u;
					}
				}
				$new_arr[]=$v;
            }
        }
		// dump($new_arr);
		// die;
		$this->assign('list',$new_arr);
        $this->display();
    }
	public function department_addD($name,$group_id){
        $this->p = M('RootWarehouse');
        if($name==''){
            alert(0,'部门名称为空');
        }
        if($group_id==''){
            alert(0,'权限不能为空');
        }
        $count=$this->p->where("name='".$name."'")->count();
        if($count>0){
            alert(0,'名称已经存在');
        }
        //////权限检查，是否具备这些权限
        $arr_id=explode(',',$group_id);
        foreach($arr_id as $v){
            if(!is_numeric($v)){
                alert('操作有误');
            }
        }
        $where['id']=array('IN',$arr_id);
        $result=$this->p->where($where)->getField('name',true);
        if(count($result)!=count($arr_id)){
            alert('操作有误');
        }
        $data['name']=$name;
        $massage=session('admin');
        $data['operator']=$massage;
        $this->product = M('DepartmentWarehouse');
        $data['root_group_id']=$group_id;
        $result=$this->product->add($data);
		// echo $this->product->getlastsql();
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function department_edit($id){
        if($id==''){
            echo "错误";
        }
		
		$root=M('RootWarehouse')->order("sort")->select();
		$this->product = M('DepartmentWarehouse');
		$this->data=$this->product->find($id);
        $root_id=$this->product->where(array('id'=>$id))->getField('root_group_id');
        $arr_root=explode(',',$root_id);
        $new_arr=array();
		foreach($root as $k=>$v){
			if(in_array($v['id'],$arr_root)){
				$v['checked']=1;
			}
            if($v['pid']==0){//顶级菜单
                foreach($root as $j=>$u){
					if(in_array($u['id'],$arr_root)){
						$u['checked']=1;
					}
					if($v['id']==$u['pid']){
						foreach($root as $js=>$us){
							if(in_array($us['id'],$arr_root)){
								$us['checked']=1;
							}
							if($u['id']==$us['pid']){
								foreach($root as $jss=>$uss){
									if(in_array($uss['id'],$arr_root)){
										$uss['checked']=1;
									}
									if($us['id']==$uss['pid']){
											$us['child'][]=$uss;
									}
								}
								$u['child'][]=$us;
							}
						}
						$v['child'][]=$u;
					}
				}
				$new_arr[]=$v;
            }
        }
        $this->assign('list',$new_arr);
        $this->display();
    }
    public function department_editD($id,$group_id){
        $this->p = M('RootWarehouse');
        if($group_id==''){
            alert(0,'权限不能为空');
        }
        //////权限检查，是否具备这些权限
        $arr_id=explode(',',$group_id);
		// dump($arr_id);
		// die;
        foreach($arr_id as $v){
            if(!is_numeric($v)){
                alert('操作有误');
            }
        }
        $where['id']=array('IN',$arr_id);
        $result=$this->p->where($where)->getField('name',true);
        if(count($result)!=count($arr_id)){
            alert('操作有误');
        }
        $massage=session('admin');
        $data['operator']=$massage;
        $this->product = M('DepartmentWarehouse');
        $data['root_group_id']=$group_id;
        $result=$this->product->where(array('id'=>$id))->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function department_del($id){
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $result=M('DepartmentWarehouse')->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }

	
}